package com.fishing.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import QL.ButlerBroadcastMessageAddRequest;
import QL.ButlerBroadcastMessageAddResponse;
import QL.ButlerBroadcastMessageGetListRequest;
import QL.ButlerBroadcastMessageGetListResponse;
import QL.ButlerBroadcastMessageRemoveRequest;
import QL.ButlerBroadcastMessageRemoveResponse;
import QL.QLClient;


/**
 * 跑马灯公告
 * @author lrz
 * */
@Controller
public class RunnoticeController {
	
	private static QLClient client = new QLClient();
	
    @RequestMapping("Runnotice/toAdd")
    public ModelAndView toAdd(HttpServletRequest request) {
    	ModelAndView mav = new ModelAndView("Runnotice/toAdd");
    	return mav;
    }
    
    @ResponseBody
    @RequestMapping(value="Runnotice/add",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public String add(HttpServletRequest request,String content,String time,String interval) {
    	ButlerBroadcastMessageAddRequest req = new ButlerBroadcastMessageAddRequest();
    	try {
    		req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
        	req.Content = content;
        	req.Interval = Integer.parseInt(interval)*60;
        	req.Duration = Integer.parseInt(time)*60;
        	req.Operater = (String)request.getSession().getAttribute("Admintruename");
		} catch (Exception e) {
			e.printStackTrace();
		}
    	ButlerBroadcastMessageAddResponse rsp = new ButlerBroadcastMessageAddResponse();
    	if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
    	if(rsp.ErrMsg == null || rsp.ErrMsg == "") return "success";
    	else return rsp.ErrMsg;
    }
    
    @RequestMapping("Runnotice/toList")
    public ModelAndView toList(HttpServletRequest request) {
    	ModelAndView mav = new ModelAndView("Runnotice/toList");
    	return mav;
    }
    
    @ResponseBody
    @RequestMapping(value="Runnotice/List",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public Map<String,Object> List(HttpServletRequest request,String pageCount,String pageIndex) {
    	ButlerBroadcastMessageGetListRequest req = new ButlerBroadcastMessageGetListRequest();
    	try {
    		req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
        	req.PageCount = Integer.parseInt(pageCount);
        	req.PageIndex = Integer.parseInt(pageIndex) - 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
    	ButlerBroadcastMessageGetListResponse rsp = new ButlerBroadcastMessageGetListResponse();
    	if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
    	Map<String,Object> map = new HashMap<>();
    	if(rsp.ErrMsg == null || rsp.ErrMsg == "") {
    		map.put("status", "200");
    		map.put("message", "");
    		map.put("count", rsp.TotalCount);
    		map.put("data", rsp.ResultList);
    		return map;
    	}else {
    		System.out.println(rsp.ErrMsg);
    		map.put("status", "200");
    		map.put("message", "");
    		map.put("count", 0);
    		map.put("data", 0);
    		return map;
    	}
    }
    
    @ResponseBody
    @RequestMapping(value="Runnotice/Remove",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public String Remove(HttpServletRequest request,String Id) {
    	ButlerBroadcastMessageRemoveRequest req = new ButlerBroadcastMessageRemoveRequest();
    	try {
    		req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
        	req.Id = Integer.parseInt(Id);
        	req.Operater = (String)request.getSession().getAttribute("Admintruename");
		} catch (Exception e) {
			e.printStackTrace();
		}
    	ButlerBroadcastMessageRemoveResponse rsp = new ButlerBroadcastMessageRemoveResponse();
    	if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
    	return rsp.ErrMsg;
    }
}
